package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.pojo.AdSensitive;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.feign.wemedia.sensitive.WmSensitiveFeignClients;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.media.dtos.SensitiveResponseDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    WmSensitiveFeignClients sensitiveFeignClients;


    /**
     * 获取敏感词文字的集合, feign调用
     *
     * @return
     */
    @Override
    public List<String> getSensitiveWord() {
        List<AdSensitive> sensitiveList = this.list();
        if (sensitiveList != null) {
            return sensitiveList.stream().map(AdSensitive::getSensitives).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 保存敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public void saveSensitive(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 调用feign接口保存到wm_sensitive中
        sensitiveFeignClients.saveSensitive(dto);
    }

    /**
     * 根据 sensitives 查询关键字
     *
     * @param sensitives
     * @return
     */
    private AdSensitive findBySensitives(String sensitives) {
        if (StringUtils.isBlank(sensitives)) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdSensitive::getSensitives, sensitives);
        return this.getOne(queryWrapper);
    }

    /**
     * 分页查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<SensitiveResponseDTO> findList(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        Page<SensitiveResponseDTO> page = sensitiveFeignClients.findList(dto);

        if (page == null) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<>(dto, page.getTotal(), page.getRecords());
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public void updateSensitive(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 调用feign接口进行敏感词的修改
        sensitiveFeignClients.updateSensitive(dto);
    }

    /**
     * 删除敏感词
     *
     * @param id
     */
    @Override
    public void deleteSensitive(Integer id) {
        if (id == null || id < 0) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 调用feign接口进行敏感词的删除
        sensitiveFeignClients.deleteSensitive(id);
    }
}
